package com.woniuxueyuan.config;

import com.woniuxueyuan.util.RabbitmqConst;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 新建队列，新建交换机，做绑定关系
 */

@Configuration
public class RabbitmqConfig {

    /**
     * 定义校验订单状态的队列
     * 队列方法名：和业务相关
     */
    @Bean
    public Queue checkOrderStatusQueue(){
        return new Queue(RabbitmqConst.CHECKORDERSTATUS_QUEUE);
    }
    @Bean
    public CustomExchange delayExchange() {
        Map map = new HashMap();
        //指定交换机类型
        map.put("x-delayed-type","topic");
        /**
         * 参数1：交换机名字
         * 餐数2：交换机类型
         * 参数3：是否持久化
         * 参数4：是否自动删除
         * 参数5：指定其他参数
         */
        return new CustomExchange(RabbitmqConst.DELAY_EXCHANGE,"x-delayed-message",true,false,map);
    }
    @Bean
    public Binding checkOrderStatusQueueToDelayExchange(Queue checkOrderStatusQueue,CustomExchange delayExchange){
        return BindingBuilder.bind(checkOrderStatusQueue).to(delayExchange).with("delay").noargs();
    }
    /**
     * 添加秒杀订单队列
     */
    @Bean
    public Queue addKillOrderQueue(){
        return new Queue(RabbitmqConst.ADDKILLORDER_QUEUE);
    }
    @Bean
    public TopicExchange topicExchange(){
        return new TopicExchange(RabbitmqConst.TOPIC_EXCHANGE);
    }
    @Bean
    public Binding addKillOrderQueueToTopicExchange(Queue addKillOrderQueue,TopicExchange topicExchange){
        return  BindingBuilder.bind(addKillOrderQueue).to(topicExchange).with("killOrder");
    }

    }

